package com.shiwaixiangcun.monitor.service.impl;

import com.shiwaixiangcun.core.exceptions.MonkeyException;
import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.service.BaseServiceImpl;
import com.shiwaixiangcun.core.utils.MonkeyUtils;
import com.shiwaixiangcun.monitor.domain.User;
import com.shiwaixiangcun.monitor.domain.WorkOrderCategory;
import com.shiwaixiangcun.monitor.domain.WorkOrderQualityAllotRule;
import com.shiwaixiangcun.monitor.dto.WorkOrderQualityAllotRuleDto;
import com.shiwaixiangcun.monitor.repository.WorkOrderQualityAllotRuleRepository;
import com.shiwaixiangcun.monitor.service.UserService;
import com.shiwaixiangcun.monitor.service.WorkOrderCategoryService;
import com.shiwaixiangcun.monitor.service.WorkOrderQualityAllotRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/23.
 */
@Service
@Transactional
public class WorkOrderQualityAllotRuleServiceImpl extends BaseServiceImpl<WorkOrderQualityAllotRule, Long> implements WorkOrderQualityAllotRuleService {

    @Autowired
    private WorkOrderQualityAllotRuleRepository workOrderQualityAllotRuleRepository;
    @Autowired
    private WorkOrderCategoryService workOrderCategoryService;
    @Autowired
    private UserService userService;

    @Override
    public List<WorkOrderQualityAllotRuleDto> ruleList() {
        return toDto(findAll());
    }

    @Override
    public void ruleAdd(MonkeyDto monkeyDto) {
        WorkOrderQualityAllotRule rule = buildAddRule(monkeyDto);
        repeatCheck(rule);
        save(rule);
    }

    @Override
    public void ruleRemove(Long id) {
        delete(id);
    }

    @Override
    public WorkOrderQualityAllotRule getUsableRule(WorkOrderCategory category) {
        return workOrderQualityAllotRuleRepository.findByCategory(category);
    }

    private WorkOrderQualityAllotRule buildAddRule(MonkeyDto monkeyDto) {
        WorkOrderQualityAllotRule rule = new WorkOrderQualityAllotRule();
        WorkOrderCategory category = workOrderCategoryService.findOne(monkeyDto.getLong("categoryId"));
        User user = userService.findOne(monkeyDto.getLong("userId"));
        if (MonkeyUtils.isEmpty(category)) {
            throw new MonkeyException("该工单分类不存在");
        }
        if (MonkeyUtils.isEmpty(user)) {
            throw new MonkeyException("该质检人不存在");
        }
        rule.setCategory(category);
        rule.setUser(user);
        return rule;
    }

    private void repeatCheck(WorkOrderQualityAllotRule rule) {
        WorkOrderQualityAllotRule rs = workOrderQualityAllotRuleRepository.findByCategory(rule.getCategory());
        if (MonkeyUtils.isNotEmpty(rs)) {
            throw new MonkeyException("存在相同规则");
        }
    }

    private List<WorkOrderQualityAllotRuleDto> toDto(List<WorkOrderQualityAllotRule> list) {
        List<WorkOrderQualityAllotRuleDto> dtoList = new ArrayList<>();
        WorkOrderQualityAllotRuleDto dto;
        for (WorkOrderQualityAllotRule rule : list) {
            dto = new WorkOrderQualityAllotRuleDto();
            dto.setId(rule.getId());
            dto.setUserName(rule.getUser().getName());
            dto.setCategoryName(rule.getCategory().getName());
            dtoList.add(dto);
        }
        return dtoList;
    }
}
